
import React from 'react';
import { Drawer, Form, Select, Space, Button } from 'antd';

import { OPTION_LXLS_ENV } from '../core/common'
import { EnvFillHoder, FillHepler } from '../core/fill'

class FillEnvChooser extends React.Component {
    constructor(props) {
        super(props);

        this.handleSubmit = this.handleSubmit.bind(this);
        this.onFinish = this.onFinish.bind(this);
        this.onFinishFailed = this.onFinishFailed.bind(this);

        this.formRef = React.createRef();
    }

    handleSubmit() {
        this.formRef.current.submit();
    }
    onFinish(values) {
        console.log('Success:', values);
        const envName = values.env_name;
        const fill = new EnvFillHoder({ name: envName });
        const helper = new FillHepler(window.luckysheet, this.props.config.book_type);
        helper.setCellValue(0, 0, fill);
        this.props.onComplete();

    };
    onFinishFailed(errorInfo) {
        console.log('Failed:', errorInfo);
    };

    render() {
        const { open, onCancel } = this.props;
        return (
            <Drawer title="变量填充"
                width={550}
                zIndex={1020}
                open={open}
                onClose={onCancel}
                extra={
                    <Space>
                        <Button onClick={onCancel}>取消</Button>
                        <Button onClick={this.handleSubmit} type="primary">
                            确定
                        </Button>
                    </Space>
                }>
                <Form
                    colon={false}
                    ref={this.formRef}
                    onFinish={this.onFinish}
                    onFinishFailed={this.onFinishFailed}>

                    <Form.Item
                        label="变量类型"
                        name="env_name"
                        rules={[
                            {
                                required: true,
                            },
                        ]}>
                        <Select
                            options={OPTION_LXLS_ENV} />
                    </Form.Item>
                </Form>

            </Drawer>
        )
    }
}

export default FillEnvChooser